﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Mozlite.Data;

namespace Mozlite.Components
{
    /// <summary>
    /// 多语言配置基类。
    /// </summary>
    /// <typeparam name="TConfiguration">配置类型。</typeparam>
    [Serializable]
    public abstract class BaseThemeResourceConfiguration<TConfiguration> : BaseResourceConfiguration<TConfiguration>, IThemeResourceConfiguration
        where TConfiguration : BaseThemeResourceConfiguration<TConfiguration>, new()
    {
        /// <summary>
        /// 初始化类<see cref="BaseThemeResourceConfiguration&lt;TConfiguration&gt;"/>。
        /// </summary>
        public BaseThemeResourceConfiguration()
        {

        }

        /// <summary>
        /// 获取缓存键。
        /// </summary>
        /// <returns>返回缓存键。</returns>
        protected override string GetCacheKey()
        {
            return "ThemeResourceConfigurations[{2}][{1}][{0}]".FormatWith(ConfigurationKey, CultureName, ThemeName);
        }

        /// <summary>
        /// 从数据库中加载配置。
        /// </summary>
        /// <param name="configuration">加载的配置实例对象。</param>
        public override void LoadConfiguration(ref TConfiguration configuration)
        {
            using (var reader = Database.ExecuteReader("SELECT * FROM moz_themes_configurations WHERE [Key] = @Key AND [CultureName] = @CultureName AND [ThemeName] = @ThemeName", new { Key = ConfigurationKey, CultureName = CultureName, ThemeName = ThemeName }))
            {
                if (reader.Read())
                {
                    configuration = Cores.Deserialize<TConfiguration>(reader.GetString("SettingsXML"));
                    configuration.CreateDate = reader.GetDateTime("CreateDate");
                    configuration.LastUpdateDate = reader.GetDateTime("LastUpdateDate");
                }
                reader.Close();
            }
        }

        /// <summary>
        /// 获取配置。
        /// </summary>
        /// <param name="theme">模板名称。</param>
        /// <param name="cultureName">当前语言。</param>
        /// <returns>返回配置实例对象。</returns>
        public static TConfiguration GetConfiguration(string theme, string cultureName)
        {
            TConfiguration configuration = null;
            using (var reader = Database.ExecuteReader("SELECT * FROM moz_themes_configurations WHERE [Key] = @Key AND [CultureName] = @CultureName AND [ThemeName] = @ThemeName", new { Key = ConfigurationKey, CultureName = cultureName, ThemeName = theme }))
            {
                if (reader.Read())
                {
                    configuration = Cores.Deserialize<TConfiguration>(reader.GetString("SettingsXML"));
                    configuration.CreateDate = reader.GetDateTime("CreateDate");
                    configuration.LastUpdateDate = reader.GetDateTime("LastUpdateDate");
                }
                reader.Close();
            }
            return configuration;
        }

        /// <summary>
        /// 保存当前配置。
        /// </summary>
        /// <returns>返回保存结果。</returns>
        public override bool Save()
        {
            if (Database.ExecuteNonQuery(@"
IF EXISTS(SELECT 1 FROM moz_themes_configurations WHERE [Key] = @Key AND [CultureName] = @CultureName AND [ThemeName] = @ThemeName) BEGIN
    UPDATE moz_themes_configurations SET [SettingsXML] = @SettingsXML, [LastUpdateDate] = GETDATE() WHERE [Key] = @Key AND [CultureName] = @CultureName AND [ThemeName] = @ThemeName;
END ELSE BEGIN
    INSERT INTO moz_themes_configurations([Key], [CultureName], [CreateDate], [LastUpdateDate], [SettingsXML], [ThemeName])VALUES(@Key, @CultureName, GETDATE(), GETDATE(), @SettingsXML, @ThemeName);
END", new { SettingsXML = Cores.Serialize((TConfiguration)this), Key = ConfigurationKey, CultureName = CultureName, ThemeName = ThemeName }))
            {
                Caches.Remove(this.GetCacheKey());
                return true;
            }
            return false;
        }

        /// <summary>
        /// 当前模板名称。
        /// </summary>
        [XmlIgnore]
        public virtual string ThemeName { get { return "default"; } }
    }
}
